"""
python操作MySQL数据插入
"""
import pymysql
# 连接MySQL数据库
conn = pymysql.connect(
    host='localhost',
    user='root',
    password='111246',
    autocommit=True,
)
# print(conn.get_server_info())
# 创建游标
cursor = conn.cursor()
# 创建数据库
cursor.execute("create database if not exists test")
# 选择数据库
conn.select_db("test")
# 创建数据表
cursor.execute(
    'create table if not exists student(id int, name varchar(20), age int)'
)
# 插入数据
cursor.execute("insert into student(id) values(1)")
cursor.execute("insert into student(id, name, age) values(2, '周杰伦', 10)")
# 多条插入
data = [
    (3, '林俊杰', 18),
    (4, '王力宏', 20),
    (5, '陶喆', 22),
]
cursor.executemany("insert into student(id, name, age) values(%s, %s, %s)", data)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()